package com.yilin.medical.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.utils.AppUtil;
import com.yilin.medical.utils.JxSystemUtil;

/**
 * 视频会诊详情页,这个页面,没有listview,所以在这里没有使用bean和adapter
 * 
 * @author lijianqun
 * 
 */
public class WoDeZhuanZhenDetailsActivity extends BaseActivity {
	private Context context;
	private TextView  mTextViewJieShou, mTextViewJuJue;
	private ScrollView mScrollView;
	private int showflag = 0,title =0;
	private LinearLayout mLinearLayoutJuJueLiYou;
	private RelativeLayout mRelativeLayoutXuanZe;
	private int statusBarHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentViewWithTitle(R.layout.activity_wodezhuanzhendetails);
		statusBarHeight = JxSystemUtil.getStatusBarHeight(this);
		showflag = getIntent().getIntExtra("showflag", 0);
		title = getIntent().getIntExtra("title", 0);
		initView();
		registener();
	}

	private void initView() {
		mTextViewJieShou = (TextView) findViewById(R.id.tv_jieshou);
		mTextViewJuJue = (TextView) findViewById(R.id.tv_jujue);
		if (title==1) {
			mTitle.getTitle().setText("我的会诊详情");
		} else {
			mTitle.getTitle().setText("我的转诊详情");
		}
		mScrollView = (ScrollView) findViewById(R.id.sv);
		mLinearLayoutJuJueLiYou = (LinearLayout) findViewById(R.id.ll_jujueliyou);
		mRelativeLayoutXuanZe = (RelativeLayout) findViewById(R.id.rl_xuanze);
		if (showflag == 0) {
			mLinearLayoutJuJueLiYou.setVisibility(View.VISIBLE);
			mRelativeLayoutXuanZe.setVisibility(View.GONE);
		}
		if (showflag == 1) {
			mLinearLayoutJuJueLiYou.setVisibility(View.GONE);
			mRelativeLayoutXuanZe.setVisibility(View.GONE);
		}
		if (showflag == 2) {
			mLinearLayoutJuJueLiYou.setVisibility(View.GONE);
			mRelativeLayoutXuanZe.setVisibility(View.VISIBLE);
		}
	}

	private void registener() {
		mTextViewJieShou.setOnClickListener(this);
		mTextViewJuJue.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.tv_jieshou:
			// 弹出接受的dialog
			showJieShowPop();
			break;
		case R.id.tv_jujue:
			// 弹出拒绝理由的dialog
			showJuJuePop();
			break;
		}
	}

	private void showJieShowPop() {
		View layout = ((Activity) context).getLayoutInflater().inflate(
				R.layout.pop_wodezhuanzhenjieshou, null);
		final PopupWindow pop = new PopupWindow(layout,
				AppUtil.getScreenWidth(context),
				AppUtil.getScreenHeight(context), false);
		pop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);
		if (JxSystemUtil.isFlymeOs4x()) {
			pop.setHeight(AppUtil.getScreenHeight(context) - statusBarHeight
					* 2);
			// 至于这里为什么乘以2，我只能说，除了鬼鬼们，只有魅族自己知道，我是发现获得的StatusBarHeight偏小，所以乘以2，实践检验是正确的，但也很无语
			pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);
		} else {
			pop.setHeight(AppUtil.getScreenHeight(context) - statusBarHeight);
			pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);
		}
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
				0);
		ta.setDuration(500);
		layout.startAnimation(ta);
	}

	private void showJuJuePop() {
		View layout = ((Activity) context).getLayoutInflater().inflate(
				R.layout.pop_wodezhuanzhenjujue, null);
		final PopupWindow pop = new PopupWindow(layout,
				AppUtil.getScreenWidth(context),
				AppUtil.getScreenHeight(context), false);
		pop.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);
		if (JxSystemUtil.isFlymeOs4x()) {
			pop.setHeight(AppUtil.getScreenHeight(context) - statusBarHeight
					* 2);
			// 至于这里为什么乘以2，我只能说，除了鬼鬼们，只有魅族自己知道，我是发现获得的StatusBarHeight偏小，所以乘以2，实践检验是正确的，但也很无语
			pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);
		} else {
			pop.setHeight(AppUtil.getScreenHeight(context) - statusBarHeight);
			pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);
		}

		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
				0);
		ta.setDuration(500);
		layout.startAnimation(ta);
	}

	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		params.height += 5;
		listView.setLayoutParams(params);
	}

}
